package com.felix.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.shop.bussiness.ContentBuilderFactory;
import com.shop.util.RequestArgs;


/**
 * 所有请求的入口。
 * @author Felix Einzbern
 *
 */
public class Auto extends HttpServlet {
	
	private static final long serialVersionUID = -6767098866769127707L;
	private static Logger log = Logger.getLogger(Auto.class);
	private static Logger iplog = Logger.getLogger("ip");
	
	private static String[] cts=new String[]{"application/octet-stream; charset=UTF-8","text/plain; charset=UTF-8","image/jpeg"};
	//@Override
	public void init() throws ServletException {

	}

	//@Override
	public void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {

		String[] url =null;
		String[] remoteips=null;
//		java.io.OutputStream out=response.getOutputStream();
//		final DataOutputStream dos2 = new DataOutputStream(new BufferedOutputStream(out));
//		request.setCharacterEncoding("utf-8");

		long start=System.currentTimeMillis();
		try {
			Map<String,String[]> Parameter = (Map<String,String[]>) request.getParameterMap();
			Map<String,String[]> ParameterMap = new HashMap<String,String[]>();
			String RequestURL = request.getRequestURI(); // 得到本机的URL
			String QureyString = request.getQueryString(); // URL参数
			url= new String[] { RequestURL + "?" + QureyString };

			String funid = request.getParameter("funid");
			String tid=request.getParameter("tid");


			// 得到页面数据和图片数据
			String buffer = null;
			RequestArgs args=new  RequestArgs(Parameter);
			if(buffer==null) {
				buffer = ContentBuilderFactory.getContent(args);  //返回的数据
			}
			if (buffer != null) {//正常页面
				if ("4".equals(funid)) {	//发布页或者预订处理页      返回推荐列表页
					if (buffer.indexOf("error")==-1) {
					response.setCharacterEncoding("utf-8");
					response.setContentType("text/html;charset=UTF-8"); 
					response.sendRedirect(buffer);
					return;
					}
				}
				if ("11".equals(funid)&&"2".equals(tid)) {
					if (buffer.indexOf("error")==-1) {
						response.setCharacterEncoding("utf-8");
						response.setContentType("text/html;charset=UTF-8"); 
						response.sendRedirect(buffer);
						return;
					}

				}
				if ("6".equals(funid)&&"0".equals(tid)) {  //乘客取消预订 、 车主踢掉乘客
					if (buffer.indexOf("error")==-1) {
						response.setCharacterEncoding("utf-8");
						response.setContentType("text/html;charset=UTF-8"); 
						response.sendRedirect(buffer);
						return;
					}

				}
				response.setContentType("text/xml;charset=UTF-8");
				PrintWriter pw=response.getWriter();
				pw.write(buffer);
				pw.flush();
				pw.close();
				return;


			} else // 页面错误代码
			{
				log.error("本次业务处理异常，没有返回数据");
			}
		}catch (Throwable e) {
			log.error("remoteip=");
		}
		finally {
			long end=System.currentTimeMillis();
			log.debug("end-----------------------------------------------times="+(end-start)+" ms");
		}

	}

	//@Override
	public void doPost(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
		this.doGet(request, response);
	}

	//@Override
	public void destroy() {
	}

}
